package com.egao.orderB.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.orderB.mapper.PutStorageMapper;
import com.egao.orderB.biz.PutGoodsBiz;
import com.egao.orderB.biz.PutInfoBiz;
import com.egao.orderB.biz.StockBiz;
import com.egao.orderB.entity.*;
import com.egao.orderB.service.PutStorageService;
import org.springframework.stereotype.Service;
import com.egao.orderB.biz.PutStorageBiz;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库信息服务实现类
 * @author : Doman
 * @date : 2021-12-23 15:43:48
 * @version ：1.0
 */
@Service
public class PutStorageServiceImpl /*extends ServiceImpl<PutStorageMapper, PutStorage> */implements PutStorageService {

	@Autowired
	private PutStorageBiz putStorageBiz;
	@Autowired
	private PutGoodsBiz putGoodsBiz;
	@Autowired
	private PutInfoBiz putInfoBiz;
	@Autowired
	private StockBiz stockBiz;

  @Override
  public JsonResult list(PageResult2<T> page, PutStorage putStorage, User loginUser)  throws Exception{
    return putStorageBiz.list(page,putStorage,loginUser);
  }

  @Override
  public JsonResult list13(PageResult2<T> page, PutStorageAndInfo putStorageAndInfo, User loginUser)  throws Exception{
    return putStorageBiz.list13(page,putStorageAndInfo,loginUser);
  }

  @Override
  public JsonResult list(PutStorage putStorage, User loginUser) {
    return JsonResult.ok().setData(putStorageBiz.list(putStorage,loginUser));
  }

  @Override
  public JsonResult listVolume(PutStorage putStorage, User loginUser) {
    return JsonResult.ok().setData(putStorageBiz.listVolume(putStorage,loginUser));
  }

  @Override
  public JsonResult listMonth(PutStorage putStorage, User loginUser) {
    return JsonResult.ok().setData(putStorageBiz.listMonth(putStorage,loginUser));
  }

  @Override
  public JsonResult getById(PutStorage putStorage) {
    return JsonResult.ok().setData(putStorageBiz.getById(putStorage));
  }

  @Override
  public JsonResult get13(PutStorageAndInfo putStorageAndInfo) {
    return JsonResult.ok().setData(putStorageBiz.get13(putStorageAndInfo));
  }

  @Transactional
  @Override
  public JsonResult saveStorage(String putStorage, String putGoodsList, String putInfo, User loginUser) {
    PutStorage putStorage1 = com.alibaba.fastjson.JSONObject.parseObject(putStorage, PutStorage.class);
//    PutGoods putGoods1 = com.alibaba.fastjson.JSONObject.parseObject(putGoods, PutGoods.class);
    List<PutGoods> putGoodsList2 =  JSON.parseArray(putGoodsList, PutGoods.class);
    PutInfo putInfo1 = JSONObject.parseObject(putInfo, PutInfo.class);
    int id = putStorageBiz.save(putStorage1, loginUser);
    for (PutGoods putGoods : putGoodsList2) {
      putGoods.setPutInStorageId(id);
      putGoods.setCustomerId(putInfo1.getCustomerId());
      putGoods.setCustomer(putInfo1.getCustomer());
      putGoodsBiz.save(putGoods, loginUser);
      Stock stock = new Stock();
      stock.setCustomerId(putInfo1.getCustomerId());
      stock.setCustomer(putInfo1.getCustomer());
      stock.setSku(putGoods.getSku());
      stock.setName(putGoods.getName());
      stock.setNum(putGoods.getNum());
      stock.setPack(putGoods.getPack());
      stock.setWeight(putGoods.getWeight());
      stock.setLength(putGoods.getLength());
      stock.setWidth(putGoods.getWidth());
      stock.setHigh(putGoods.getHigh());
//      stock.setVolume(putGoods.getVolume());
//      stock.setTotalVolume(putGoods.getTotalVolume());
      List<Stock> stocks = stockBiz.listUnique(stock);
      if (stocks != null && stocks.size() > 0) {
        stock.setId(stocks.get(0).getId());
        stockBiz.updateNum(stock, loginUser);
      } else {
        stockBiz.save(stock, loginUser);
      }
    }
    putInfo1.setPutInStorageId(id);
    putInfoBiz.save(putInfo1, loginUser);
    return JsonResult.ok("添加成功");
  }

  @Transactional
  @Override
  public JsonResult updateStorage13(String putStorage, String putInfo, User loginUser) {
    PutStorage putStorage1 = com.alibaba.fastjson.JSONObject.parseObject(putStorage, PutStorage.class);
//    PutGoods putGoods1 = com.alibaba.fastjson.JSONObject.parseObject(putGoods, PutGoods.class);
    PutInfo putInfo1 = JSONObject.parseObject(putInfo, PutInfo.class);
    putStorageBiz.update(putStorage1, loginUser);
    putInfoBiz.update(putInfo1, loginUser);
    return JsonResult.ok("修改成功");
  }

  @Transactional
  @Override
  public JsonResult save(PutStorage putStorage, User loginUser) {
    int id = putStorageBiz.save(putStorage, loginUser);
    return JsonResult.ok("添加成功");
  }

  @Transactional
  @Override
  public JsonResult update(PutStorage putStorage, User loginUser) {
    if (putStorageBiz.update(putStorage, loginUser)) {
      return JsonResult.ok("修改成功");
    }
    return JsonResult.error("修改失败");
  }

  @Transactional
  @Override
  public JsonResult remove(PutStorage putStorage, User loginUser) {
    String[] strs = putStorage.getId().split(",");
    for (String id : strs) {
      if(id==null || id.equals("")){ continue; }
      putStorage = new PutStorage();
      putStorage.setId(id);
      if (putStorageBiz.remove(putStorage, loginUser)) {
        PutGoods putGoods = new PutGoods();
        putGoods.setPutInStorageId(Integer.valueOf(putStorage.getId()));
        List<PutGoods> putGoodsList = putGoodsBiz.list(putGoods,loginUser);
        if (putGoodsList != null && putGoodsList.size() > 0) {
          for (PutGoods putGoodsOld : putGoodsList) {
            if (putGoodsBiz.remove(putGoodsOld, loginUser)) {
              PutStorage putStorage2 = new PutStorage();
              putStorage2.setId(putGoodsOld.getPutInStorageId()+"");
              putStorage2.setNum(0 - putGoodsOld.getNum());;
              putStorage2.setTotalGrossWeight((0-putGoodsOld.getTotalWeight())+"");
              putStorage2.setVolumeM3((0-putGoodsOld.getTotalVolume())+"");
              putStorageBiz.updateNum(putStorage2, loginUser);

              Stock stock = new Stock();
              stock.setCustomerId(putGoodsOld.getCustomerId());
              stock.setSku(putGoodsOld.getSku());
              stock.setName(putGoodsOld.getName());
              stock.setPack(putGoodsOld.getPack());
              stock.setWeight(putGoodsOld.getWeight());
              stock.setLength(putGoodsOld.getLength());
              stock.setWidth(putGoodsOld.getWidth());
              stock.setHigh(putGoodsOld.getHigh());
              List<Stock> stockListUnique = stockBiz.listUnique(stock);
              stock = stockListUnique.get(0);
              stock.setNum(0 - putGoodsOld.getNum());
              stockBiz.updateNum(stock, loginUser);
            }
          }
        }
      }
    }
    return JsonResult.ok("删除成功");
  }

}
